#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@IDE     ：PyCharm 
@Author  ：kiway
@Date    ：2024/11/3 19:28 
"""

import asyncio
import aiozmq
import zmq
import zmq.asyncio

async def publisher():
    # 创建一个ZMQ上下文和一个PUB套接字
    context = zmq.asyncio.Context()
    socket = context.socket(zmq.PUB)
    socket.bind('tcp://127.0.0.1:5555')  # 绑定到特定端口

    print("Publisher started, sending messages...")
    await asyncio.sleep(1)  # 等待订阅者连接

    while True:
        # 发布消息
        message = "Hello, World!  1"
        await socket.send_string(f"topic {message}")
        print(f"Sent: topic {message}")
        await asyncio.sleep(1)  # 每秒发送一次消息

if __name__ == "__main__":
    asyncio.run(publisher())
